﻿'
' Copyright (c) 2010-2012, MatthiWare
'
' Permission is hereby granted, free of charge, to any person obtaining a
' copy of this software and associated documentation files (the "Software"),
' to deal in the Software without restriction, including without limitation
' the rights to use, copy, modify, merge, publish, distribute, sublicense,
' and/or sell copies of the Software, and to permit persons to whom the
' Software is furnished to do so, subject to the following conditions:
'
' The above copyright notice and this permission notice shall be included in
' all copies or substantial portions of the Software. 
' You shall include 'MatthiWare' in the credits/about section of the program
'
' THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
' IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
' FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
' AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
' LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
' FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
' DEALINGS IN THE SOFTWARE.
'
Imports SharpDX

Public Class WorldItem

    Property Item() As Item

    Public X As Int32 = 0
    Public Y As Int32 = 0

    Public rect As DrawingRectangleF

    Private pt As Point

    Public Sub New(x As Int32, y As Int32)
        MyBase.New()
        Me.pt = New Point(x, y)
        Me.X = x
        Me.Y = y
    End Sub

    Public Sub Tick(e As Core.Base.GameCoreEventArgs)
        Dim p As Player = e.Core.Services.GetService(GetType(Player))
        Dim cam As Camera = p.Cam
        Me.X = (pt.X - cam.X) * Tile.TileSize
        Me.Y = (pt.Y - cam.Y) * Tile.TileSize
        Dim world As World = e.Core.Services.GetService(GetType(World))
        Dim underBlock As Block = world.level(pt.X, pt.Y + 1)
        If (underBlock.Info.Name = "Air") Then
            pt.Y += 1
        End If

        Dim w As Int32 = Item.ItemImageCollection(Item.Type).Size.Width
        Dim h As Int32 = Item.ItemImageCollection(Item.Type).Size.Height

        rect = New DrawingRectangleF(Me.X - 50, Me.Y - 50, w + 100, h + 100)

        If (Utils.Contains(rect, p.rect)) Then
            ' can be picked up

            Dim inventory As Inventory = p.inventory
            Dim invslot As InventorySlot = inventory.GetSlotFor(Item.Type)
            If (Not invslot.Info.Item.Type = Me.Item.Type) Then
                invslot.Info.Item = Item.LoadItem(Me.Item.Type, e)
            End If
            invslot.Info.Stack += 1

            world.ItemCollection.Remove(Me)

        End If

    End Sub

End Class
